// 引入 QCloud 小程序增强 SDK
var qcloud = require('../../../../vendor/qcloud-weapp-client-sdk/index');

// 引入配置
var config = require('../../../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => {
    wx.hideToast();
    wx.showToast({
        title: text,
        icon: 'success'
    });
};

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};
var showMsg = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: content,
        showCancel: false
    });
};
//获取应用实例
var app = getApp()
Page({
  data: {
  "imageSrc":'',
  "text":'',
  "is_qun":0,
  "busy1":false,
  "busy2":false,
  "textarea":false,
  "sizeType":['original'],
  "expire":false,
  "imageAble":false,
  "upload_busy":false
  },
  onLoad: function () {
    console.log('onLoad');
    app.getUserInfo(function(userInfo){
      console.log(userInfo);
    });
    const ctx = wx.createCanvasContext('myCanvas')
    var self=this;
    var res = wx.getSystemInfoSync();
    if(res.system.indexOf('iOS')!==-1){
      self.setData({
          'sizeType':['compressed']
      });
    }
    self.chooseImage();
  },
  onShow: function () {
    console.log('onShow');
    // this.chooseImage();
  },
  chooseImage: function() {

    var self = this;
    console.log(self.data.sizeType);
    if(self.data.upload_busy||self.data.textarea){
      console.log(self.data.textarea)
      return;
    }
    wx.chooseImage({
      count: 1,
      sizeType: self.data.sizeType,
      sourceType: ['album','camera'],
      success: function(res) {
        //  showMsg('提示信息','chooseImage successful,temp path is'+res.tempFilePaths[0]);
        console.log(res);
        console.log('chooseImage success, temp path is', res.tempFilePaths[0])
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success: function (res) {
            console.log(res)
          }
        })
        var imageSrc = res.tempFilePaths[0];
        // wx.saveFile({
        //   tempFilePath: imageSrc,
        //   success: function(res) {
        //     console.log(res)
        //   }
        // })
        // wx.getSavedFileList({
        //   success: function(res) {
        //     console.log(res.fileList)
        //   }
        // })
        self.setData({
            'upload_busy':true
        })
        //上传图片到服务器
        wx.uploadFile({
          url: config.service.uploadUrl,
          filePath: imageSrc,
          name: 'file',
          success: function(res) {
            console.log(res.statusCode);
            if(res.statusCode==200){
              var data=JSON.parse(res.data);
              console.log(data);
              if(data.success==true){
                self.setData({
                    'imageSrc':data.file.url,
                    'imageAble':false
                })
                showSuccess('上传成功');
              }else{
                showMsg('提示信息','上传失败');
              }
            }else if(res.statusCode==413){
              showMsg('提示信息','上传失败,文件过大');
            }else{
              showMsg('提示信息','上传失败');
            }
          },
          fail: function({errMsg}) {
            console.log('uploadImage fail, errMsg is', errMsg);
            showMsg('提示信息','上传失败,'+errMsg);
          },
          complete:function(){
            self.setData({
                'upload_busy':false
            })
          }
        })

      },
      fail: function({errMsg}) {
        // showMsg('提示信息','chooseImage fail, err is'+errMsg)
      }
    })
  },
  expire_set:function(e){
    console.log(e.target.dataset);
  },
  formSubmit1: function(e) {
    // console.log(e);
    
    var self = this;
    if(self.data.upload_busy||self.data.busy1){
      return;
    }
    if(self.data.imageSrc==""&&self.data.text==""){
      showMsg("提示信息","文字和图片至少不为空");
      return;
    }
    var data={};
    if(self.data.imageSrc!=""){
      data.image_url=self.data.imageSrc;
    }
    if(self.data.text!=""){
      data.text=self.data.text;
    }
    if(e.detail.formId!='the formId is a mock one'){
      data.form_id=e.detail.formId;
    }
    data.type=2
    data.is_qun=1
    self.setData({
        'busy1':true
    })
    qcloud.request({
            // 要请求的地址
            url: config.service.messageAddUrl,
            method:"POST",
            data: data,
            success(result) {
              console.log(result);
              if(result.data.success==true){
                 showSuccess("成功");
                 wx.navigateTo({
                    url: '/pages/person/image/view/view?hash_key='+result.data.id
                 })
              }else{
                showMsg("提示信息","新建消息失败，"+result.data.message);
              }
            },

            fail(error) {
                console.log('request fail', error);
                showMsg("提示信息","新建消息失败");
            },

            complete() {
                self.setData({
                      'busy1':false
                  })
                console.log('request complete');
            }
      });
  },
  formSubmit2: function(e) {
    // console.log(e);
    
    var self = this;
    if(self.data.upload_busy||self.data.busy2){
      return;
    }
    if(self.data.imageSrc==""&&self.data.text==""){
      showMsg("提示信息","文字和图片至少不为空");
      return;
    }
    var data={};
    if(self.data.imageSrc!=""){
      data.image_url=self.data.imageSrc;
    }
    if(self.data.text!=""){
      data.text=self.data.text;
    }
    if(e.detail.formId!='the formId is a mock one'){
      data.form_id=e.detail.formId;
    }
    data.type=2
    data.is_qun=0
    self.setData({
        'busy2':true
    })
    qcloud.request({
            // 要请求的地址
            url: config.service.messageAddUrl,
            method:"POST",
            data: data,
            success(result) {
              console.log(result);
              if(result.data.success==true){
                 showSuccess("成功");
                 wx.navigateTo({
                    url: '/pages/person/image/view/view?hash_key='+result.data.id
                 })
              }else{
                showMsg("提示信息","新建消息失败，"+result.data.message);
              }
            },

            fail(error) {
                console.log('request fail', error);
                showMsg("提示信息","新建消息失败");
            },

            complete() {
                self.setData({
                      'busy2':false
                  })
                console.log('request complete');
            }
      });
  },
  textarea_toggle:function(){
    if(this.data.textarea==false){
        this.setData({
            'textarea':true
        })
    }else{
      this.setData({
          'textarea':false
      })
    }
  },
  bindTextAreaInput:function(e){
    console.log(e.detail.value)
    this.setData({
        'text':e.detail.value
    })
  },
  bindTextAreaConfirm:function(e){
    console.log(e.detail.value)
    this.setData({
        'text':e.detail.value
    })
  },
  imageLoad: function(e){
    console.log(e);
    this.setData({
        'imageAble':true
    })
    var width=e.detail.width;
    var height=e.detail.height;
    try {
      var res = wx.getSystemInfoSync()
      if(res.system.indexOf('Android')!==-1){
        if(width>height){
          this.setData({
            'imageSrc':this.data.imageSrc,
          })
        }
      }
    } catch (e) {
      // Do something when catch error
    }
  }
})
